﻿using System;
using System.Collections;
using viBlog.Classes;
using viBlog.Controllers;
using visinia.SmartEngine;

public partial class Admin_Pages_viBlog_DataFilters_DataFilters : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["ModuleId"] == null)
            return;
        WebPageController.AddScriptLink(Page, WebRoots.GetAdminPagesRoot + "viBlog/DataFilters/DataFilters.js");
        if (!IsPostBack)
        {
            int ModuleId = Convert.ToInt32(Request.QueryString["ModuleId"]);
            viBlogDataFilter filter = DataFilterController.GetFilter(ModuleId);
            if (filter == null)
            {
                int count = 0; // a default value
                COUNT.Value = count.ToString();
                return;
            }
            else
            {
                switch (filter.FilterType)
                {
                    case DataFilterController.FILTER.LATEST_BY_DATE:
                        Filters.SelectedValue = "viBlogDF_LATEST";
                        break;
                    case DataFilterController.FILTER.LATEST_BY_CATEGORY:
                        Filters.SelectedValue = "viBlogDF_CATEGORY";
                        CATEGORIES.SelectedValue = filter.Data;
                        break;
                    case DataFilterController.FILTER.LATEST_BY_TAG:
                        Filters.SelectedValue = "viBlogDF_TAGS";
                        TAGS.SelectedValue = filter.Data;
                        break;
                    case DataFilterController.FILTER.POSTS_LIST:
                        Filters.SelectedValue = "viBlogDF_POSTS_LIST";
                        POSTS_LIST.Value = filter.Data;
                        break;
                }
                COUNT.Value = filter.ItemsCount.ToString();
            }
        }
    }

    public void SaveFilterData(Hashtable settings)
    {
        string selCOUNT = (string)settings["viBlogDF_COUNT"];
        string selFILTER = (string)settings["viBlogDF_FILTERS"];
        string selCATEGORY = (string)settings["viBlogDF_CATEGORY"];
        string selTAGS = (string)settings["viBlogDF_TAGS"];
        string selPOSTS_LIST = (string)settings["viBlogDF_POSTS_LIST"];

        int ModuleId = Convert.ToInt32(settings["MODULE_ID"]);
        viBlogDataFilter filter = DataFilterController.GetFilter(ModuleId);
        if (filter == null)
        {// create a new filter
            filter = new viBlogDataFilter();
            filter.ModuleId = ModuleId;
            int temp = 0;
            if (Int32.TryParse(selCOUNT, out temp))
                filter.ItemsCount = temp;
            if (selFILTER == "viBlogDF_LATEST")// show latest posts
                filter.FilterType = DataFilterController.FILTER.LATEST_BY_DATE;
            else if (selFILTER == "viBlogDF_CATEGORY")//show latest by categories
            {
                filter.FilterType = DataFilterController.FILTER.LATEST_BY_CATEGORY;
                filter.Data = selCATEGORY;
            }
            else if (selFILTER == "viBlogDF_TAGS")//show latest by tags
            {
                filter.FilterType = DataFilterController.FILTER.LATEST_BY_TAG;
                filter.Data = selTAGS;
            }
            else if (selFILTER == "viBlogDF_POSTS_LIST")//show posts spedified in the list
            {
                filter.FilterType = DataFilterController.FILTER.POSTS_LIST;
                filter.Data = selPOSTS_LIST;
            }
            DataFilterController.AddFilter(filter);
        }
        else
        {// update already existing filter
            int temp = 0;
            if (Int32.TryParse(selCOUNT, out temp))
                filter.ItemsCount = temp;

            if (selFILTER == "viBlogDF_LATEST")// show latest posts
                filter.FilterType = DataFilterController.FILTER.LATEST_BY_DATE;
            else if (selFILTER == "viBlogDF_CATEGORY")//show latest by categories
            {
                filter.FilterType = DataFilterController.FILTER.LATEST_BY_CATEGORY;
                filter.Data = selCATEGORY;
            }
            else if (selFILTER == "viBlogDF_TAGS")//show latest by tags
            {
                filter.FilterType = DataFilterController.FILTER.LATEST_BY_TAG;
                filter.Data = selTAGS;
            }
            else if (selFILTER == "viBlogDF_POSTS_LIST")//show posts spedified in the list
            {
                filter.FilterType = DataFilterController.FILTER.POSTS_LIST;
                filter.Data = selPOSTS_LIST;
            }
            DataFilterController.UpdateFilter(filter);
        }
    }
}
